<template>
	<div class="container add_edit">
		<el-form v-loading="formLoading" :disabled="addBtnLoading" :model="form" :rules="rules" ref="ruleForm"
			label-width="100px">
			<el-form-item label="分享标题" prop="share_title" ref="share_title">
				<el-input v-model="form.share_title" placeholder="请输入分享标题" clearable>
				</el-input>
			</el-form-item>
			<el-form-item label="分享图标">
				<uni-file-picker :disabled="addBtnLoading" :disable-preview="addBtnLoading" :del-icon="!addBtnLoading"
					v-model="imageValue" fileMediatype="image" returnType="object" :image-styles="imageStyle"
					@success="imgUpload" @delete="imgDelete" />
				<div class="remark_txt">宽度 400px，高度 400px，大小建议几十KB左右</div>
			</el-form-item>
			<el-form-item label="分享描述">
				<el-input type="textarea" :autosize="{ minRows: 3 }" v-model="form.share_desc" placeholder="请输入分享描述">
				</el-input>
			</el-form-item>
			<el-form-item label="分享状态">
				<el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" active-value="启用"
					inactive-value="禁用"></el-switch>
			</el-form-item>
			<el-form-item>
				<el-button round icon="el-icon-check" :loading="addBtnLoading" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'usemall-app-share';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				form: {
					share_title: '',
					share_img: '',
					share_desc: '',
					state: '启用'
				},
				imageValue: null,
				imageStyle: {
					height: '150px',
					width: '150px'
				},
				rules: {
					share_title: [{
						required: true,
						message: '请输入分享标题',
						trigger: 'change'
					}]
				},
				addBtnLoading: false
			};
		},
		methods: {
			async loadData() {
				this.formLoading = true;
				await this.$db[__name].tofirst({
					loading: false
				}).then(res => {
					if (res.code == 200) {
						res.datas = res.datas || {};
						this.dataId = res.datas._id || '';

						if (Object.keys(res.datas).length) {
							for (let item in this.form) {
								this.form[item] = res.datas[item];
							}

							if (this.form.share_img) {
								this.imageValue = {};
								this.imageValue.url = this.form.share_img;
							}
						}
					}
					this.formLoading = false;
				});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.addBtnLoading = true;
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						} else {
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						}
					}
				});
			},
			imgUpload(e) {
				this.form.share_img = e.tempFilePaths[0];
			},
			imgDelete() {
				this.imageValue = null;
				this.form.share_img = '';
			}
		},
		created() {
			this.loadData();
		}
	};
</script>

<style></style>